<?php
namespace app\api\controller;
use think\Cache;
use think\Controller;

class Wx 
{	

 
	// public $appid =config('appid');
	// private $appsecret;
	// private $access_token;

	// public function __construct($appid, $appsecret, $access_token)
	// {
	// 	$this->appid = $appid;
	// 	$this->appsecret = $appsecret;
	// 	$this->access_token = $access_token;
	// }
	
    public function valid()
    {
        $echoStr = $_GET["echostr"];
        if($this->checkSignature()){
            header('content-type:text');
            echo $echoStr;
            exit;
        }
    }

    private function checkSignature()
    {
    	define("TOKEN", "weixin");
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];

        $token = TOKEN;
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode( $tmpArr );
        $tmpStr = sha1( $tmpStr );

        if( $tmpStr == $signature ){
            return true;
        }else{
            return false;
        }
    }
 //CURL 请求
   public function https_request($url,$data = null){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL,$url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)){
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }

    //获取微信Token
     public function getWxAccessToken()
    {
    	//$appid = config('appid');
    	$appid='wx0c35cc8848113e96';
    	//$appsecret = config('appsecret');
    	$appsecret='d56c7f1d8b72e287bdb23459fa2697ac';
    	$curl = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$appsecret;
    	$res = $this->https_request($curl);
    	$res = json_decode($res);
    	//dump($res);
    	$access_token = $res->access_token;
    	Cache::set('access_token', $access_token, 7200);
    	
    }

    public function responseMsg()
    {
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

        if (!empty($postStr)){
            $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
            $fromUsername = $postObj->FromUserName;
            $toUsername = $postObj->ToUserName;
            $keyword = trim($postObj->Content);
            $time = time();
            $textTpl = "<xml>
                        <ToUserName><![CDATA[%s]]></ToUserName>
                        <FromUserName><![CDATA[%s]]></FromUserName>
                        <CreateTime>%s</CreateTime>
                        <MsgType><![CDATA[%s]]></MsgType>
                        <Content><![CDATA[%s]]></Content>
                        <FuncFlag>0</FuncFlag>
                        </xml>";
            if($keyword == "?" || $keyword == "？")
            {
                $msgType = "text";
                $contentStr = date("Y-m-d H:i:s",time());
                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                echo $resultStr;
            }
        }else{
            echo "";
            exit;
        }
    }

   
    

    

   

    //创建自定义菜单
    public function create_menu()
    {
    	 // "type":"view",
      //     "name":"个人中心",
      //     "url":"http://www.myriadintl.com/tp5/public/"
      
    	$this->getWxAccessToken();
    	$access_token=Cache::get('access_token');
    	echo $access_token;
    	$appid='wx0c35cc8848113e96';
    	$appsecret='d56c7f1d8b72e287bdb23459fa2697ac';
    	$curl = 'https://api.weixin.qq.com/cgi-bin/menu/create?access_token='.$access_token;
    	$auto_url=urlencode("http://www.bdjianshen.com/oauth2.php");
    	$button = array(
    		'button'=>array(
                [	//第一个一级菜单
                	'name'=>urlencode('预约场馆'),
                	'sub_button'=>array(
                			[
                			'name'=>urlencode('投诉建议'),	
                			'type'=>'view',
                			'url'=>'http://www.bdjianshen.com/bdjianshen/public/index.php/api/advice/index'
                			],
                			[
                			'name'=>urlencode('关于BD'),
                			'type'=>'view',
                			'url'=>'http://www.bdjianshen.com/bdjianshen/public/index.php/api/about/about'
                			],
                			[
                			'name'=>urlencode('最新资讯'),
                			'type'=>'view',
                			'url'=>'http://www.imooc.com'
                			],
                	    [
                	    'name'=>urlencode('商家端入口'),
                	    'type'=>'view',
                	    'url'=>'http://www.bdjianshen.com/bdjianshen/public/index.php/seller/Login'
                	        ],
                			[
                			'name'=>urlencode('附近场馆'),
                			'type'=>'view',
                			'url'=>'http://www.bdjianshen.com/cesi2.php'
                			],

                			)
                ],
                //第二个一级菜单
                [
                	'name'=>urlencode('我的健身'),
                	'sub_button'=>array(
                			[
                			'name'=>urlencode('个人信息'),	
                			'type'=>'view',
                			'url'=>"https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=$auto_url&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect"
                			],
                			[
                			'name'=>urlencode('资金账户'),
                			'type'=>'view',
                			'url'=>'http://www.bdjianshen.com/bdjianshen/public/index.php/api/Myaccount/index'
                			],
                			[
                			'name'=>urlencode('系统消息'),
                			'type'=>'view',
                			'url'=>'http://www.bdjianshen.com/bdjianshen/public/index.php/api/Message/SysMessageFetch'
                			],
                			[
                			'name'=>urlencode('收藏评价'),
                			'type'=>'view',
                			'url'=>'http://www.bdjianshen.com/bdjianshen/public/index.php/api/Myevaluation/selectlists'
                			],
                			[
                			'name'=>urlencode('我的预定'),
                			'type'=>'view',
                			'url'=>'http://www.bdjianshen.com/bdjianshen/public/index.php/api/Venueappoint/selectLists'
                			],

                		)
                ],//

                )
    		);
    	$postArr = urldecode(json_encode($button));
    	echo $access_token;
    	echo '<hr>';
    	echo $postArr;
    	$res = $this->https_request($curl,$postArr);
    	dump($res);

    }
    public function test_access_token(){
        $appid='wx0c35cc8848113e96';
        $appsecret='d56c7f1d8b72e287bdb23459fa2697ac';
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;
        $res=$this->https_request($url);
        file_put_contents('ss', $res);
      
        dump($res);
    }


}

